package muse;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import muse.Frame.MFrame;
import muse.frames.Customize;
import muse.frames.Facebook;
import muse.frames.Help;
import muse.frames.Journal;
import muse.frames.Muse;
import muse.frames.Museum;
import muse.frames.Portal;
import muse.frames.Splash;
import muse.frames.CharacterCustomize;
import muse.widgets.RoundButton;

/*
 * The main window for the application.  This class acts as a container
 * to display and manipulate each screen.
 */
public class Window extends JPanel {
	/* Initialize one instance of each screen */
	private static HashMap<MFrame, Frame> frames;
	static {
		frames = new HashMap<MFrame, Frame>();
		frames.put(MFrame.CUSTOMIZE, new Customize());
		frames.put(MFrame.HELP, new Help());
		frames.put(MFrame.JOURNAL, new Journal());
		frames.put(MFrame.MAIN, new Muse());
		frames.put(MFrame.FACEBOOK, new Facebook());
		frames.put(MFrame.MUSEUM, new Museum());
		frames.put(MFrame.SPLASH, new Splash());
		frames.put(MFrame.PORTAL, new Portal());
                frames.put(MFrame.CHARACTERCUSTOMIZE, new CharacterCustomize());
	}
	/* The root pane for everything else.  By using a 
	 * JLayeredPane, the navigator can pop up on top of
	 * the main content without having to do any annoying 
	 * hacks. */
	private JLayeredPane layeredPane;
	/* The JPanels representing the active Frame and the 
	 * Navigator, respectively.  */
	private JPanel content, navi;
	
	public Window(int width, int height) {
		super();
		setSize(width, height);
		layeredPane = new JLayeredPane();
		layeredPane.setSize(width, height);
		add(layeredPane);
		/* Add each Frame, but make it invisible.
		 * They're all on the bottom layer. */
		for(Frame f : frames.values()) {
			layeredPane.add(f, new Integer(0));
			f.setVisible(false);
		}
		navi = new Navigator();
		/* Add the navigator on top of the Frames. */
		layeredPane.add(navi, new Integer(1));
		
                
		/* Make the navigator invisible. */
		toggleNavi();
		
		/* Change the mouse cursor to whatever custom 
		 * cursor image. */
		configureCursor();
	}
	
	/*
	 * Loads a custom cursor image from the jar and
	 * sets that image to display whenever the mouse
	 * is inside the Muse window.
	 */
	private void configureCursor() {
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image cursorImage = tk.getImage(getClass().getClassLoader().getResource("muse/resources/blue_dot.png"));
		Cursor cursor = tk.createCustomCursor(cursorImage, new Point(1,1), "Custom Cursor");
		setCursor(cursor);
	}
	
	public Dimension getPreferredSize() {
		return new Dimension(320,480);
	}
	
	/*
	 * Hide the previous Frame and show another.
	 * 
	 * @param	next	The MFrame representing the screen to display.
	 */
	public void changeScreen(MFrame next) {
		/* A quick check just in case there isn't a Frame being 
		 * displayed yet. */
		if(content != null) {
			((Frame)content).freeze();
			content.setVisible(false);
		}
		content = frames.get(next);
		content.setVisible(true);
		((Frame)content).init();
		/* This just avoids issues with the display not 
		 * automatically refreshing. Stupid.*/
		validate();
		revalidate();
		repaint();
		layeredPane.setPreferredSize(new Dimension(320,480));
	}
	
	/*
	 * Toggles whether or not the Navigator panel is visible
	 * or not.  
	 * 
	 * @return	The new visibility of the Navigator.
	 */
	public boolean toggleNavi() {
		navi.setVisible(!navi.isVisible());
		validate();
		return navi.isVisible();
	}
	
	/*
	 * A simple Panel that just holds each button to change screens.
	 */
	private class Navigator extends JPanel {
		private Navigator() {
			/* Sizing stuff. */
			setBounds(0, (int)(layeredPane.getHeight() * 0.85), layeredPane.getWidth(), layeredPane.getHeight() - (int)(layeredPane.getHeight() * .77));
			setPreferredSize(new Dimension(layeredPane.getWidth(), (int)getBounds().getHeight()));
			setLayout(new FlowLayout());
			/* Add a button for each Frame that has been registered. */
			for(final MFrame f : MFrame.values()) {
				/* We don't need a button for the Splash screen. */
				if(f == MFrame.SPLASH || f == MFrame.PORTAL || f == MFrame.CHARACTERCUSTOMIZE)
					continue;
				JButton button = new JButton(f.getText());
				/* TODO: Figure out how to dynamically handle font size
				 * based on window size? */
				button.setFont(new Font("Times New Roman", Font.PLAIN, 13));
				/* TODO: The button sizing formula needs to be adjusted. */
				button.setPreferredSize(new Dimension((getWidth() - 20) / 3, getHeight() / 4));
				/* Clicking a button should cause the screen to be 
				 * changed.*/
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						changeScreen(f);
					}
				});
				add(button);
			}
		}
	}
}
